<template>
	<view class="mainbgc">
		<view class="addMoney-container mainpadding">
			<view>
				<input type="text" v-model="payprice" placeholder="请输入充值金额" class="inp">
			</view>
		</view>
		<view class="btn">
			<view type="primary" class="btn-container" @click="lijipaymoney">立即充值</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				payprice: '',
				list: [],
				wxpay: {},
				type:0
			}
		},
		onLoad(optios) {
			let _this = this
			// #ifdef MP-WEIXIN
			const url = decodeURIComponent(optios.q) // 获取到二维码原始链接内容
			if (url.indexOf("?") != -1) {
				var str = url.substr(url.indexOf("?") + 1);
				var strs = str.split("&");
				let theRequest = {};
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
				}
				_this.type = optios.type ? optios.type : (theRequest.type ? theRequest.type :
					0);
			}
			// #endif
		},
		methods: {
			lijipaymoney() {
				let _this = this;
				if (!httpRequest.checkIsLogin()) {
					httpRequest.doLogin();
					return false;
				}
				httpRequest.request('api/rechargeorder/rechargeMoney', 'POST', {
					pay_price: _this.payprice
				}, false, false, true).then(res => {
					if (res.code == 1) {
						this.wxpay = JSON.parse(res.data.wxconfig)
						this.callPayMent()
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				
				})
			},
			callPayMent() {
				let _this = this;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: _this.wxpay.timeStamp,
					nonceStr: _this.wxpay.nonceStr,
					package: _this.wxpay.package,
					signType: _this.wxpay.signType,
					paySign: _this.wxpay.paySign,
					success: function(res) {
						httpRequest.toast("充值成功")
						// 支付成功
						setTimeout(()=>{
							if(_this.type==0){
								uni.navigateBack(1)
							}else{
								uni.reLaunch({
									url: "/pages/hotel/jiudian"
								})
							}
						},500)
					},
					fail: function(err) {
						// 支付失败
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.inp {
		display: block;
		padding: 30rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}
</style>
